/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.classic;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.paint.iDrawable;

public class SelectedLettersDrawable extends iDrawable {
  private final Word myWord;
  private int myColor;
  private WordBattleView myWordBattleView;
  private Paint myPaint = new Paint();
  private float myMinSize;
  
  public SelectedLettersDrawable( Word aWord, int aColor ) {
    myWord = aWord;
    myColor = aColor;
    init();
  }
  
  private void init(){
    myMinSize = (float)(Math.min(myWordBattleView.getHeightPerRow(), myWordBattleView.getWidthPerColumn()) * 0.7 );
    myPaint.setColor( myColor );
    myPaint.setStrokeWidth( myMinSize );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(true);
  }
  
  @Override
  public void paint( Canvas aCanvas, View aView ) {
    myWordBattleView = (WordBattleView)aView;
//    myPaint.setColor(Color.RED);
    Letter thePreviousLetter = null;
    float thePreviousCenterX = -1;
    float thePreviousCenterY = -1;
    for(Letter theLetter : myWord.getLetters()){
      
      float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * myWordBattleView.getWidthPerColumn());
      float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * myWordBattleView.getHeightPerRow());
      aCanvas.drawCircle(theCenterX, theCenterY, myMinSize / 2, myPaint);
      
      if(thePreviousLetter != null){
        //draw a rectangle from the center of the previsou letter to the center of this letter
        aCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
      }
      
      thePreviousLetter = theLetter;
      thePreviousCenterX = theCenterX;
      thePreviousCenterY = theCenterY;
    }
  }
}
